package com.finance.cooperate.strategy.core;

import com.finance.cooperate.common.pojo.RestResult;
import com.finance.cooperate.strategy.common.params.StrategyParams;
import com.finance.cooperate.strategy.core.common.ConfigureProcessor;
import com.finance.cooperate.strategy.core.strategy.StrategyScope;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @ClassName StrategyProcessorHolder
 * @Description 策略执行主体
 * @Author shenanxin
 * @Date 2023/8/23 15:51
 * @Modify ...
 */
@Slf4j
@Component
public class StrategyProcessorHolder {

    @Autowired
    private StrategyScope strategyScope;

    @Autowired
    @Qualifier("strategyConfig")
    private ConfigureProcessor config;


    public RestResult run(StrategyParams params) {

        Double finalScore = params.getScore();

        // 1. 生成流水号
        String recodeId = UUID.randomUUID().toString().replace("-", "");


        // 2. 初始化参数封装
        strategyScope.setUid(params.getUid());
        strategyScope.setApplyId(params.getApplyId());
        strategyScope.setModelId(params.getModelId());
        strategyScope.setLoanNum(params.getLoanNum());
        strategyScope.setRecodeId(recodeId);
        strategyScope.setInitialScore(finalScore);
        strategyScope.setFinalScore(finalScore);
        strategyScope.setFeatureMap(params.getFeatureMap());


        // 3. 策略执行
        config.run();


        // 策略执行完成，返回结果
        if (strategyScope.isHit()) { // 表示有策略命中
            finalScore = strategyScope.getFinalScore();
        }


        return RestResult.ok(finalScore);

    }

}
